package b2.widget.tech
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	import b2.widget.tank.*;
	import b2.widget.warehouse.*;
	
	import com.smartfoxserver.v2.requests.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class MixtureTab extends TechTab
	{
		public var originalSlot2:ObjectSlot;
		public var mixSlot:ObjectSlot;
		
		public function MixtureTab()
		{
			super();
			originalSlot2 = new ObjectSlot();
			originalSlot2.x = 120;
			originalSlot2.y = 100;
//			originalSlot2.width = 60;
//			originalSlot2.height = 50;
			
			mixSlot = new ObjectSlot();
			mixSlot.x = 250;
			mixSlot.y = 100;
//			fuseSlot.width = 60;
//			fuseSlot.height = 50;
			
			addChild(originalSlot);
			addChild(originalSlot2);
			addChild(mixSlot);
			
			//箭头
			var RightArrow:Class = ResourceHelper.getInstance().getClassByName("RightArrow2");
			var rightArrow:Sprite = new RightArrow();
			rightArrow.x = 178;
			rightArrow.y = 110;
			addChild(rightArrow);
			
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			startButton = new BasicButton(ButtonBgr, "立即熔炼");
			startButton.x = 223;
			startButton.y = 375;
			startButton.disable = true;
			addChild(startButton);
			startButton.addEventListener(MouseEvent.CLICK, onStartButtonClick);
			
		}
		
		private function onStartButtonClick(event:MouseEvent):void
		{
			if (startButton.disable)
				return;
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var originalObjConfig:CommonObjConfig = Config.objectConfigs[originalSlot.objectIndex];
			var objBalance:String = this.generateObjBalance();
			var successRate:int = parseInt(successRateText2.text);
			var successFlag:Boolean;
			if (Math.random() < successRate / 100)
				successFlag = true;
			else
				successFlag = false;
			//坦克
			if (originalObjConfig.type == 0)
			{
				if (originalSlot2.tankId == me.campTankId)
					ServerHelper.getInstance().mixtureTank(originalSlot2.tankId, originalSlot.tankId, mixSlot.objectIndex, 
						mixSlot.basicLevel, mixSlot.strengthenLevel, objBalance, successFlag);
				else 
				    ServerHelper.getInstance().mixtureTank(originalSlot.tankId, originalSlot2.tankId, mixSlot.objectIndex, 
				    	mixSlot.basicLevel, mixSlot.strengthenLevel, objBalance, successFlag);
				ServerHelper.getInstance().addEventListener(ServerEvent.MIXTURE_TANK, mixtureComplete);
			}
			removeAll();
		}
		
		private function mixtureComplete(event:ServerEvent):void
		{
			var messageBox:PopMessage;
			if (event.result == "success")
			{
				messageBox = new PopMessage("熔炼成功！",0x00ff00);
			}
			else
			{
				messageBox = new PopMessage("熔炼失败！",0xff0000);
			}
			WidgetManager.getInstance().popOut([messageBox], false, true);
			Warehouse.getInstance().refresh();
		}
		
		public override function loadObject(objectIndex:int, basicLevel:int, strengthenLevel:int, tankId:int = 0):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var object:CommonObjConfig = Config.objectConfigs[objectIndex];
			if (!originalSlot.hasObj)
			{
				originalSlot.loadObj(objectIndex, basicLevel, strengthenLevel, 1, 1, tankId);
				if (object.type == 0)
					me.tankPackage.deleteTank(tankId);
				else
					me.objPackage.objectList[originalSlot.comIndex]--;
			}
			else if (!originalSlot2.hasObj)
			{
				originalSlot2.loadObj(objectIndex, basicLevel, strengthenLevel, 1, 1, tankId);
				if (object.type == 0)
					me.tankPackage.deleteTank(tankId);
				else
					me.objPackage.objectList[originalSlot.comIndex]--;
			}
			else
			{
				removeAll();
				originalSlot.loadObj(objectIndex, basicLevel, strengthenLevel, 1, 1, tankId);
				if (object.type == 0)
					me.tankPackage.deleteTank(tankId);
				else
					me.objPackage.objectList[originalSlot.comIndex]--;
			}
			var objectConfig:FormulaConfig = getFormulaConfig(originalSlot.objectIndex, originalSlot2.objectIndex);
			if (!originalSlot2.hasObj)
			{
				warningTip.text = "需要两件物品才能熔炼!";
				startButton.disable = true;
			}
			else if (objectConfig == null)
			{
				warningTip.htmlText = "无效熔炼公式！" + "<a target='_blank' href = '"+Profile.URL_MIXTURE_PAGE+"'> <font color='#00FF00' size='12'>(点击查看公式)</font></a>";
				startButton.disable = true;
			}
			else
			{
				var totalLevel:int = (originalSlot.basicLevel * 5 + originalSlot.strengthenLevel + originalSlot2.basicLevel * 5 + originalSlot2.strengthenLevel) / 2;
				var resultBasicLevel:int = totalLevel / 5;
				var resultStrengthenLevel:int = totalLevel - 5 * resultBasicLevel;
				mixSlot.loadObj(objectConfig.resultTank, resultBasicLevel, resultStrengthenLevel, 1, 1, tankId);
				warningTip.text = "";
				startButton.disable = false;
			}
			loadMaterial();
		}
		 
		protected override function refreshSuccessRate():void
		{
			if (!originalSlot.hasObj)
				return;
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var activatorNum:int = me.getObjNum(CommonObjConfig.ACT, 0, 0);
			numSel.max = activatorNum;
			var objectConfig:FormulaConfig = getFormulaConfig(originalSlot.objectIndex, originalSlot2.objectIndex);
			if (objectConfig)
			{
				var basicLevel:int = mixSlot.basicLevel;
				var strengthenLevel:int = mixSlot.strengthenLevel;
				var successRate:int = parseInt(objectConfig.successRate[basicLevel * 5 + strengthenLevel]) + Config.basicConfig.ratePerActivator * numSel.value;
				successRateText2.text = successRate + "%";
			}
		}

		private function getFormulaConfig(index1:int, index2:int):FormulaConfig
		{
			var objectConfig:FormulaConfig = Config.formulaConfigs[index1+"+"+index2];
			if (objectConfig == null)
				objectConfig = Config.formulaConfigs[index2+"+"+index1];
			if (objectConfig == null)
				return null
			return objectConfig;
		}

		public override function loadMaterial():void
		{
			if (!mixSlot.hasObj)
				return;
			var objectConfig:FormulaConfig = getFormulaConfig(originalSlot.objectIndex, originalSlot2.objectIndex);
			if (objectConfig == null)
				return
			var basicLevel:int = mixSlot.basicLevel;
			var strengthenLevel:int = mixSlot.strengthenLevel;
			var materialIndex:Array = objectConfig.materialIndex;
			var materialNum:Array = objectConfig.materialNum;
			for (var i:int = 0; i < materialIndex.length; i++)
			{
				if (materialIndex[i] >= 0)
				{
					var materialConfig:ObjectConfig = Config.objectConfigs[materialIndex[i]];
					var bLevel:int = basicLevel;
					var sLevel:int = strengthenLevel;
					if (materialConfig.noBasicLevel)
					{
						bLevel = 0;
						sLevel = 0;
					}
					else if (materialConfig.noStrengthenLevel)
					{
						bLevel = basicLevel;
						sLevel = 0;
					}
					(objectSlots[i] as ObjectSlot).loadObj(materialIndex[i], bLevel, sLevel, -1, materialNum[i][basicLevel * 5 + strengthenLevel], 
						0, true, true);
				}
			}
			refreshSuccessRate();
			if (!hasEnoughMaterial)
			{
				warningTip.text = "需要的材料不足！";
				startButton.disable = true;
			}
		}

		public override function removeAll():void
		{
			super.removeAll();
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			if (originalSlot.hasObj)
			{
				var originalObjConfig:CommonObjConfig = Config.objectConfigs[originalSlot.objectIndex];
				if (originalObjConfig.type == 1 || originalObjConfig.type == 2)
				{
					me.objPackage.objectList[originalSlot.comIndex]++;
				}
				else if (originalObjConfig.type == 0)
				{
					me.tankPackage.tankList.push(originalSlot.tankId);
				}
				originalSlot.removeObj();
			}
			if (originalSlot2.hasObj)
			{
				var originalObjConfig2:CommonObjConfig = Config.objectConfigs[originalSlot2.objectIndex];
				if (originalObjConfig2.type == 1 || originalObjConfig2.type == 2)
				{
					me.objPackage.objectList[originalSlot2.comIndex]++;
				}
				else if (originalObjConfig2.type == 0)
				{
					me.tankPackage.tankList.push(originalSlot2.tankId);
				}
				originalSlot2.removeObj();
			}
			mixSlot.removeObj();
		}
	}
}